/**
 * 全局插件配置资源（vue插件部分）
 * @module utils/plugin
 */
import * as ElIcons from '@element-plus/icons-vue'
import draggable from './directives/draggable'
import hasPermi from './directives/hasPermi'
import waterMarker from './directives/waterMarker'
import waves from './directives/waves'
import clipboard from './directives/clipboard'
import showTip from './directives/showTip'
import router from '@/router'
/**模块store数据 */
import Com from '@s/com'
import Dict from '@s/dict'
import Guarder from '@s/guarder'
import User from '@s/user'
/**方法 */
import { selectDictLabel } from './util'

export default function (app) {
	// svg字体图标
	const svgModules = import.meta.glob('../assets/svg/*.svg')
	for (const key of Object.keys(svgModules)) {
		User().svgIcons.push(/^(?:\.\.\/assets\/svg\/)(\S+)(?:\.svg)/gi.exec(key).at(1))
	}
	for (const [key, component] of Object.entries(ElIcons)) {
		app.component(key, component)
		User().ElIcons.push(key)
	}
	/**
	 * 全局指令
	 */
	app.directive('draggable', draggable)
	app.directive('hasPermi', hasPermi)
	app.directive('waterMarker', waterMarker)
	app.directive('waves', waves)
	app.directive('clipboard', clipboard)
	app.directive('showTip', showTip)
	/**
	 * 原型链全局挂载方法
	 */
	window['$router'] = app.config.globalProperties.$router = router
	window['$store'] = app.config.globalProperties.$store = {
		com: Com(),
		dict: Dict(),
		guarder: Guarder(),
		user: User()
	}

	window['selectDictLabel'] = app.config.globalProperties.selectDictLabel = selectDictLabel
	window['resetForm'] = app.config.globalProperties.resetForm = function (refName) {
		if (refName) {
			// 不是公用的my-form标签的ref逻辑
			refName?.resetFields?.() || refName?.$refs?.refMyForm?.resetFields?.()
		}
	}
	/**
	 * vite动态加载图片静态文件
	 * @param {string} path 文件路径
	 * @returns
	 */
	const modules = import.meta.glob('../assets/img/**/*', { eager: true })
	app.config.globalProperties.getImgUrl = (path) => {
		if (path && modules) {
			if (path.includes('../assets/img/')) {
				return modules[path].default
			} else {
				return modules[`../assets/img/${path}`].default
			}
		}
	}
}
